package com.sinoglobal.sinostore.activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.bean.BaseVo;
import com.sinoglobal.sinostore.system.Constants;

/**
 * @author LK
 * @title: PublicConsultActivity.java
 * @package com.sinoglobal.lntv.activity.shop
 * @description: TODO(发表咨询)
 * @date 2014-12-1 下午3:19:37
 */
public class PublicConsultActivity extends StateBaseActivity implements OnClickListener {
    private EditText etConsult;
    private TextView tvShowNum;

    private String goodsId;//商品Id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleView.setText("发表咨询");
        titleRightText.setText("提交");
        titleRightText.setTextColor(getResources().getColor(R.color.title_bg_red));
        setContentView(R.layout.shop_activity_public_consult);
        openChenJInShi();
        initView();

        initData();
    }

    private void initData() {
        goodsId = getIntent().getStringExtra("goodsId");
    }

    private void initView() {
        etConsult = (EditText) this.findViewById(R.id.et_consult_content);
        tvShowNum = (TextView) this.findViewById(R.id.tv_text_num);
        tvShowNum.setText("50");
        baseButRight.setOnClickListener(this);
        etConsult.addTextChangedListener(new TextWatcher() {

            @SuppressLint("NewApi")
            @Override
            public void onTextChanged(CharSequence charSequence, int arg1, int arg2, int arg3) {
                int num = 50 - charSequence.length();
                tvShowNum.setText(String.valueOf(num));
                if (num < 0) {
                    titleRightText.setTextColor(getResources().getColor(R.color.text_light_gray));
                    baseButRight.setClickable(false);
                } else {
                    baseButRight.setClickable(true);
                    titleRightText.setTextColor(getResources().getColor(R.color.title_bg_red));
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.shop_base_but_right) {
            String consultContent = etConsult.getText().toString().trim();
            if (TextUtils.isEmpty(consultContent)) {
                showShortToast("请输入咨询内容");
            } else {
                publicConsult(consultContent);
            }
        } else {
        }
    }

    /**
     * @param message 参数说明
     * @author LK
     * @methods publicConsult
     * @description 描述信息 发表咨询
     * @date 2015-1-15 下午3:27:28
     */
    private void publicConsult(final String message) {
        final ProgressDialog dialog = new ProgressDialog(this);
        try {
            dialog.show();
        } catch (Exception e) {
        }
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("por", "105");
        params.addQueryStringParameter("message", message);
        params.addQueryStringParameter("proIden", Constants.STOREFLAG);
        params.addQueryStringParameter("id", goodsId);
        params.addQueryStringParameter("user_name", Constants.userName);
        params.addQueryStringParameter("user_id", Constants.userCenterId);
        params.addQueryStringParameter("nike_name", Constants.nickName);
        params.addQueryStringParameter("type", Constants.CONSULT_CODE);
        httpUtils.send(HttpMethod.POST, Constants.STORE_URL, params, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                try {
                    dialog.dismiss();
                } catch (Exception e) {
                }
                showShortToast("发送失败，请重试");
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    dialog.dismiss();
                } catch (Exception e) {
                }
                String result = responseInfo.result;
                if (!TextUtils.isEmpty(result)) {
                    BaseVo entity = JSON.parseObject(result, BaseVo.class);
                    if (Constants.STORE_SUCCESS_CODE.equals(entity.getCode())) {
                        showShortToast("发送成功，请等待客服处理!");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                PublicConsultActivity.this.finish();
                            }
                        }, 2000);
                    }
                } else {
                    showShortToast("发送失败，请重试");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
